package com.atguigu.utils;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
//import com.artofsolving.jodconverter.DocumentConverter;
//import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
//import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
//import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter; 
public class OpenOfficeUtil {
	
	
    /**
     * 转换文件成pdf
     * 
     * @param fromFileInputStream:
     * @throws IOException 
     */
//    public static String file2pdf(InputStream fromFileInputStream, String toFilePath,String type) throws Exception{
//    	Date date = new Date();
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
//        String timesuffix = sdf.format(date);
//        String docFileName = null;
//        String pdfFileName = null;
//        if("doc".equals(type)){
//            docFileName = timesuffix.concat(".doc");
//            pdfFileName = timesuffix.concat(".pdf");
//        }else if("docx".equals(type)){
//            docFileName = timesuffix.concat(".docx");
//            pdfFileName = timesuffix.concat(".pdf");
//        }else if("xls".equals(type)){
//            docFileName = timesuffix.concat(".xls");
//            pdfFileName = timesuffix.concat(".pdf");
//        }else if("xlsx".equals(type)){
//            docFileName = timesuffix.concat(".xlsx");
//            pdfFileName = timesuffix.concat(".pdf");
//        }else if("ppt".equals(type)){
//            docFileName = timesuffix.concat(".ppt");
//            pdfFileName = timesuffix.concat(".pdf");
//        }else if("pptx".equals(type)){
//            docFileName = timesuffix.concat(".pptx");
//            pdfFileName = timesuffix.concat(".pdf");
//        }else if("txt".equals(type)){
//            docFileName = timesuffix.concat(".txt");
//            pdfFileName = timesuffix.concat(".pdf");
//        }else{
//        	return null;
//        }
// 
//        File pdflOutputFile = new File(toFilePath + File.separatorChar + pdfFileName);
//        File docInputFile = new File(toFilePath + File.separatorChar + docFileName);
//        try {
//        	 if (pdflOutputFile.exists())  pdflOutputFile.delete();                 
//        	 pdflOutputFile.createNewFile();
//      
//        	 if (docInputFile.exists())  docInputFile.delete();
//             docInputFile.createNewFile();
//             
//             //由fromFileInputStream构建输入文件
//             int bytesRead = 0;
//             byte[] buffer = new byte[1024 * 8];
//             OutputStream os = new FileOutputStream(docInputFile);
//             while ((bytesRead = fromFileInputStream.read(buffer)) != -1)  
//     		    os.write(buffer, 0, bytesRead);     		 
//     		 os.close();
//             fromFileInputStream.close();
//             OpenOfficeConnection connection = new SocketOpenOfficeConnection("localhost",8100);
//     		 connection.connect();
//     		 // convert
//             DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
//             converter.convert(docInputFile, pdflOutputFile);
//             connection.disconnect();
//             // 转换完之后删除word文件
//             docInputFile.delete();
//        }catch (IOException e) {
//        	System.err.println("文件转换出错，请检查OpenOffice服务是否启动。");
//		}
//        return pdfFileName;
//    }
//    /**
//     * 文件转换成Html
//     * @param fromFileInputStream
//     * @param toFilePath
//     * @param type
//     * @return
//     * @throws Exception
//     */
//    public static String file2Html (InputStream fromFileInputStream, String toFilePath,String type) {
//    	Date date = new Date();
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
//        String timesuffix = sdf.format(date);
//        String docFileName = null;
//        String htmFileName = null;
//        if("doc".equals(type)){
//            docFileName = timesuffix.concat(".doc");
//            htmFileName = timesuffix.concat(".html");
//        }else if("docx".equals(type)){
//            docFileName = timesuffix.concat(".docx");
//            htmFileName = timesuffix.concat(".html");
//        }else if("xls".equals(type)){
//            docFileName = timesuffix.concat(".xls");
//            htmFileName = timesuffix.concat(".html");
//        }else if("xlsx".equals(type)){
//            docFileName = timesuffix.concat(".xlsx");
//            htmFileName = timesuffix.concat(".html");
//        }else if("ppt".equals(type)){
//            docFileName = timesuffix.concat(".ppt");
//            htmFileName = timesuffix.concat(".html");
//        }else if("pptx".equals(type)){
//            docFileName = timesuffix.concat(".pptx");
//            htmFileName = timesuffix.concat(".html");
//        }else if("txt".equals(type)){
//            docFileName = timesuffix.concat(".txt");
//            htmFileName = timesuffix.concat(".html");
//        }else{
//        	return null;
//        }
//		File htmlOutputFile = new File(toFilePath + File.separatorChar + htmFileName);
//        File docInputFile = new File(toFilePath + File.separatorChar + docFileName);
//        try {
//        	if (htmlOutputFile.exists()) htmlOutputFile.delete();
//        	htmlOutputFile.createNewFile();
//        	if (docInputFile.exists()) docInputFile.delete();
//        	docInputFile.createNewFile();
//        	 //由fromFileInputStream构建输入文件
//            int bytesRead = 0;
//            byte[] buffer = new byte[1024 * 8];
//            OutputStream os = new FileOutputStream(docInputFile);
//            while ((bytesRead = fromFileInputStream.read(buffer)) != -1)  
//            	os.write(buffer, 0, bytesRead);
//			os.close();
//	        fromFileInputStream.close();
//	        OpenOfficeConnection connection = new SocketOpenOfficeConnection("localhost",8100);
//	        connection.connect();
//	        // convert
//	        DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
//	        converter.convert(docInputFile, htmlOutputFile);
//	        connection.disconnect();
//	        // 转换完之后删除word文件
//	        docInputFile.delete();
//        }catch (IOException e) {
//        	System.err.println("文件转换出错，请检查OpenOffice服务是否启动。");
//		}
//        return htmFileName;
//	}	
}